package com.itheima.controller;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.CheckPageQueryDTO;
import com.itheima.service.CheckItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/checkitem")
@Api(tags = "检查项相关接口")
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;

    /**
     * 新增检查项
     * @param checkItem
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增检查项")
    public Result save (@RequestBody CheckItem checkItem) {
        log.info("新增检查项");
        checkItemService.save(checkItem);
        return Result.success();
    }

    /**
     * 检查项批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("检查项批量删除")
    public Result delete (@RequestParam List<Long> ids) {
        log.info("检查项批量删除");
        checkItemService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 根据主键ID进行检查项查询
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @ApiOperation("根据主键ID进行检查项查询")
    public Result<CheckItem> geyById (@PathVariable Long id) {
        log.info("根据主键ID进行检查项查询：{}",id);
        CheckItem checkItem = checkItemService.geyById(id);
        return Result.success(checkItem);
    }


    /**
     * 修改检查项
     * @param checkItem
     * @return
     */
    @PutMapping("/edit")
    @ApiOperation("修改检查项")
    public Result update (@RequestBody CheckItem checkItem) {
        log.info("修改检查项:{}",checkItem);
        checkItemService.updateById(checkItem);
        return Result.success();
    }

    /**
     * 检查项分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation("检查项分页查询")
   public Result<PageResult> page(@RequestBody QueryPageBean queryPageBean){
        log.info("检查项查询：{}", queryPageBean);
        PageResult pageResult = checkItemService.pageQuery(queryPageBean);
        return Result.success(pageResult);
    }

    /**
     * 检查项查询
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("检查项查询")
    public Result<List<CheckItem>> findAll () {
        log.info("检查项查询");
        List<CheckItem> checkItems = checkItemService.findAll();
        return Result.success(checkItems);
    }

    /**
     * 根据检查组来查询检查项
     * @param checkgroupId
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    @ApiOperation("根据检查组来查询检查项")
    public Result<List<Integer>> itemByGroupId (@RequestParam Integer checkgroupId) {
        List<Integer> ids = checkItemService.itemByGroupId(checkgroupId);
        return Result.success(ids);
    }

}
